package com.qyhl.webtv.commonlib.utils.view.editbar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * Created by Administrator on 2018/3/15.
 */

public class RippleImageView extends androidx.appcompat.widget.AppCompatImageView {
    private int MAX_ALPHA = 0;
    private int INIT_RADIUS = 0;
    private int RADIUS_DIFFERENCE = 0;
    private int ALPHA_DIFFERENCE = 0;
    private int alpha = 0;
    private int radius = 0;
    private Paint paint;

    public RippleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    private void initPaint() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.DKGRAY);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        MAX_ALPHA = 60;
        INIT_RADIUS = Math.min(w, h) / 3;
        RADIUS_DIFFERENCE = (Math.max(w, h) - INIT_RADIUS) / 30;
        ALPHA_DIFFERENCE = MAX_ALPHA / 30;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            radius = INIT_RADIUS;
            alpha = MAX_ALPHA;
            invalidate();
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (alpha > 0) {
            paint.setAlpha(alpha);
            canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, paint);
            radius += RADIUS_DIFFERENCE;
            alpha -= ALPHA_DIFFERENCE;
            invalidate();
        }
    }
}

